<template>
  <div>
    <el-card shadow="hover" class="box-card">
      <div class="mgb10">
        <el-button type="primary" size="mini" @click.stop="editMian.visible = true">新增类型</el-button>
      </div>
      <div class="form">
        <el-table :data="tableData" border stripe style="width: 100%">
          <el-table-column prop="typeName" label="任务类型" align="center"> </el-table-column>
          <el-table-column prop="sort" width="150" label="排序" align="center"> </el-table-column>
          <el-table-column width="200" label="状态" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                @change="e => statusChange(e, scope.row.id)"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center"> </el-table-column>
          <el-table-column fixed="right" width="250" label="操作" align="center">
            <template slot-scope="scope">
              <el-button @click="editGridName(scope.row)" type="warning" size="small">编辑</el-button>
              <el-button @click="delectItem(scope.row)" type="danger" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        class="pageBox"
        background
        layout="prev, pager, next"
        :current-page="currentPage"
        :total="pageTotal"
        @current-change="getList"
        @prev-click="getList"
        @next-click="getList"
      ></el-pagination>
    </el-card>
    <el-dialog
      :title="editMian.id === 0 ? '新增类型' : '编辑类型'"
      :visible.sync="editMian.visible"
      width="30%"
      @close="editMianClose"
    >
      <el-form label-width="100px" :model="editMian">
        <el-form-item label="类型名称：">
          <el-input v-model="editMian.typeName" placeholder="请输入类型名称"></el-input>
        </el-form-item>
        <el-form-item label="排序：">
          <el-input-number v-model="editMian.sort" placeholder="请输入排序"></el-input-number>
        </el-form-item>
        <el-form-item label="状态：">
          <el-switch v-model="editMian.status" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editMian.visible = false">取 消</el-button>
        <el-button type="primary" @click="editMianSub">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as api from '@/api/mainBody'
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageTotal: 0,
      popSearch: {
        firstName: '',
        idCard: '',
        phone: '',
      },
      editMian: {
        id: 0,
        visible: false,
        sort: 0,
        typeName: '',
        status: 0,
      },
    }
  },
  created() {
    this.getList(1)
  },
  methods: {
    getList(e) {
      this.currentPage = e
      let data = {
        current: e,
        limit: 10,
      }
      api.findQueryPage(data).then(res => {
        if (res.code == '200') {
          this.tableData = res.data.records
          this.pageTotal = res.data.total
        }
      })
    },
    editMianClose() {
      this.$data.editMian = this.$options.data().editMian
    },
    // 提交类型编辑新增
    editMianSub() {
      if (this.editMian.id == 0) {
        api.addType(this.editMian).then(res => {
          if (res.code == '200') {
            this.getList(1)
            this.editMianClose()
          }
        })
      } else {
        api.updateTaskType(this.editMian).then(res => {
          if (res.code == '200') {
            this.$message({
              type: 'success',
              message: '修改成功!',
            })
            this.getList(1)
            this.editMianClose()
          }
        })
      }
    },
    // 编辑
    editGridName(e) {
      Object.assign(this.editMian, e)
      this.editMian.visible = true
    },
    // 删除项目
    delectItem(e) {
      this.$confirm('是否删除此专项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          api.deleteById({ id: e.id }).then(() => {
            if (res.code == '200') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
              this.getList(1)
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          })
        })
    },
    // 修改状态
    statusChange(e, id) {
      api.updateTaskStatus({ id, status: e }).then(res => {
        if (res.code == '200') {
          this.$message({
            type: 'success',
            message: '修改成功!',
          })
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.pageBox {
  margin-top: 10px;
  text-align: center;
}
</style>
